﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class PurchaseRepository : RepositoryBase<Purchase>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="status">1:填写采购单 2：等待审核 3：采购中 4：采购完成</param>
        /// <param name="type">1.订单产品外购 2.自定义采购</param>
        /// <param name="customeName">申请人</param>
        /// <param name="productName">产品名称</param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public IQueryable<ProductTechnologyView> GetList(int status, int type, string customeName, string productName, DateTime beginTime, DateTime endTime)
        {
            //join u in dbcontext.Users
            // on u. equals pur.ApplyId
            var result = from pur in dbcontext.Purchases
                         //join cus in dbcontext.Customers
                         //on pur.SupplierId equals cus.Id
                         join u in dbcontext.Users
                         on pur.ApplyId equals u.Id
                         join op in dbcontext.OrderProducts
                         on pur.Id equals op.PurchaseId
                         join pro in dbcontext.Products
                         on op.ProductId equals pro.Id
                         join ord in dbcontext.Orders
                         on op.OrderId equals ord.Id
                         where (status == -10 || (status == -20 && pur.Status != 1) || (status == -11 && pur.Status != -1 && pur.Status != 1) || pur.Status== status)
                         && op.Status != -1
                         && (pur.Type == 1)
                         && (u.RealName.Contains(customeName))
                         && (pro.Name.Contains(productName))
                         && (pur.ApplyTime >= beginTime && pur.ApplyTime <= endTime)
                         select new ProductTechnologyView
                         {
                             PurchaseId = pur.Id,
                             ApplyTime = pur.ApplyTime,
                             PurchaseStatus = pur.Status,
                             PurchaseType = pur.Type,
                             // CustomerName = cus.Name,
                             ProductName = pro.Name,
                             ProductId = pro.Id,
                             //CreatorName = pro.CreatorName,
                             Number = pro.Number,
                             Amount = op.Amount,
                             Price = op.Price,
                             UserName = u.RealName,
                             OrderProductId = op.Id,
                             OrderId = op.OrderId.Value,
                             SendTime = ord.SendTime
                             //OrderNum = 
                         };

            //var result1 = from pur in dbcontext.Purchases
            //              join u in dbcontext.Users
            //              on pur.ApplyId equals u.Id
            //              //join cus in dbcontext.Customers
            //              //on pur.SupplierId equals cus.Id
            //              where (pur.Type == 2)
            //                    && (status == -10 || pur.Status == status)
            //                    && (u.RealName.Contains(customeName))
            //                    && (pur.PurchaseName.Contains(productName))
            //                    && (pur.ApplyTime >= beginTime && pur.ApplyTime <= endTime)
            //              select new ProductTechnologyView
            //              {
            //                  PurchaseId = pur.Id,
            //                  ApplyTime = pur.ApplyTime,
            //                  PurchaseStatus = pur.Status,
            //                  PurchaseType = pur.Type,
            //                  //CustomerName = cus.Name,
            //                  ProductName = pur.PurchaseName,
            //                  //CreatorName = pro.CreatorName,
            //                  Number = "",
            //                  Amount = pur.Amount,
            //                  Price = pur.ActualPrice,
            //                  UserName = u.RealName,
            //                  OrderProductId = Guid.Empty,
            //                  OrderId = Guid.Empty
            //              };
            return result;
            //return result.Concat(result1);
        }


        /// <summary>
        /// 合成订单查询
        /// </summary>
        /// <param name="status">1:填写采购单 2：等待审核 3：采购中 4：采购完成</param>
        /// <param name="type">1.订单产品外购 2.自定义采购</param>
        /// <param name="customeName">申请人</param>
        /// <param name="productName">产品名称</param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public IQueryable<ProductTechnologyView> GetPurList(int status, int type, string customeName, string productName, DateTime beginTime, DateTime endTime)
        {
            //join u in dbcontext.Users
            // on u. equals pur.ApplyId
            var result = from pc in dbcontext.PurchaseComposes
                         join pro in dbcontext.Products
                         on pc.ProductId equals pro.Id
                         where ((status == 1 && pc.Status != 10) || pc.Status == status)
                             //&& (pc.Type == 1)
                             && (pro.Name.Contains(productName))
                         select new ProductTechnologyView
                         {
                             PurchaseComposeId = pc.Id,
                             PurchaseStatus = pc.Status,
                             PurchaseType = pc.Type,
                             Amount = pc.Amount,
                             StandardAmount = pc.StandardAmount,
                             PurchaseArrivalAmount = pc.ArrivalAmount,
                             ApplyTime = pc.ApplyTime,
                             ProductName = pro.Name,
                             Number = pro.Number,
                             ParentId = pc.ParentId,
                             FinanceNumber = pc.FinanceNumber
                         };

            //var result1 = from pur in dbcontext.Purchases
            //              where (pur.Type == 2)
            //                    && (status == -10 || pur.Status == status)
            //                    && (pur.PurchaseName.Contains(productName))
            //                    && (pur.ApplyTime >= beginTime && pur.ApplyTime <= endTime)
            //              select new ProductTechnologyView
            //              {
            //                  PurchaseId = pur.Id,
            //                  ApplyTime = pur.ApplyTime,
            //                  PurchaseStatus = pur.Status,
            //                  PurchaseType = pur.Type,
            //                  //CustomerName = cus.Name,
            //                  ProductName = pur.PurchaseName,
            //                  //CreatorName = pro.CreatorName,
            //                  Number = "",
            //                  Amount = pur.Amount,
            //                  Price = pur.ActualPrice,
            //                  OrderProductId = Guid.Empty,
            //              };
            return result;
            //return result.Concat(result1);
        }

    }
}
